﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMP.ERP.Model;
using CMP.ERP.BLL;
using System.Data;

namespace CMP.ERP.Admin.Controllers
{
    public class QuanxianController : Controller
    {
        Role_Option1BLL bll = new Role_Option1BLL();
        //
        // GET: /Quanxian/

        public ActionResult Index()
        {
            //权限表
            //List<Options1> Olist = new List<Options1>();
            //DataTable Odata = bll.ShowOptions1();
            //foreach (DataRow item in Odata.Rows)
            //{
            //    Options1 o = new Options1();
            //    o.Option_Id = Convert.ToInt32(item[0]);
            //    o.Option_Name = item[1].ToString();
            //    Olist.Add(o);
            //}
            
            ////角色表
            //List<Roles1> Rlist = new List<Roles1>();
            //DataTable Rdata = bll.ShowRole1();
            //foreach (DataRow item in Rdata.Rows)
            //{
            //    Roles1 r = new Roles1();
            //    r.Role_Id = Convert.ToInt32(item[0]);
            //    r.Role_Name = item[1].ToString();
            //    Rlist.Add(r);
            //}

            //角色权限关联表
            //List<Role_Option1> ROlist = new List<Role_Option1>();
            //DataTable ROdata = bll.ShowRole_Option1();
            //foreach (DataRow item in ROdata.Rows)
            //{
            //    Role_Option1 ro = new Role_Option1();
            //    ro.Role_Option_Id = Convert.ToInt32(item[0]);
            //    ro.Option_Id = Convert.ToInt32(item[1]);
            //    ro.Option_Name = item[2].ToString();
            //    ro.Role_Id = Convert.ToInt32(item[3]);
            //    ro.Role_Name = item[4].ToString();
            //    ROlist.Add(ro);
            //}
            //ViewBag.Role_Id = new SelectList(Rlist, "Role_Id", "Role_Name");
            return View();
        }
        /// <summary>
        /// 角色信息
        /// </summary>
        /// <returns></returns>
        public ActionResult role()
        {
            List<Roles1> list = new List<Roles1>();
            DataTable Rdata = bll.Role();
            foreach (DataRow item in Rdata.Rows)
            {
                Roles1 r = new Roles1();
                r.Rid = Convert.ToInt32(item[0]);
                r.Rname = item[1].ToString();
                list.Add(r);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 权限信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Option(int pid)
        {
            List<Options1> list = new List<Options1>();
            DataTable Odata = bll.Option(pid);
            foreach (DataRow item in Odata.Rows)
            {
                Options1 o = new Options1();
                o.Oid = Convert.ToInt32(item["Oid"]);
                o.Oname = item["Oname"].ToString();
                list.Add(o);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 根据角色编号获取权限编号
        /// </summary>
        /// <returns></returns>
        public ActionResult getOidbyrid(int pid)
        {

            List<Role_Option1> list = new List<Role_Option1>();
            DataTable ROdata = bll.getOidbyrid(pid);
            foreach (DataRow item in ROdata.Rows)
            {
                Role_Option1 ro = new Role_Option1();
                ro.Oid = Convert.ToInt32(item["Oid"]);
                list.Add(ro);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 根据角色编号获取权限编号
        /// </summary>
        /// <returns></returns>
        public ActionResult getOidbyrid1(int pid)
        {
            List<Role_Option1> list = new List<Role_Option1>();
            DataTable ROdata = bll.getOidbyrid(pid);
            foreach (DataRow item in ROdata.Rows)
            {
                Role_Option1 ro = new Role_Option1();
                ro.Oid = Convert.ToInt32(item["Oid"]);
                list.Add(ro);
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 修改权限
        /// </summary>
        /// <returns></returns>
        public int update(int pid, string str)
        {
            List<Role_Option1> list = new List<Role_Option1>();
            DataTable ROdata = bll.getOidbyrid(pid);
            foreach (DataRow item in ROdata.Rows)
            {
                Role_Option1 ro = new Role_Option1();
                ro.Oid = Convert.ToInt32(item["Oid"]);
                list.Add(ro);
            }
            string[] Oid = str.Split(',');//分割成数组
            //判断当前角色下面有无权限
            if (list.Count == 0)
            {
                for (int i = 0; i < Oid.Length; i++)
                {
                    bll.add(pid, Oid[i]);
                }
                return 1;
            }
            if (list.Count > 0)
            {
                bll.update(pid);//删除之前的权限
                //循环添加新权限
                for (int i = 0; i < Oid.Length; i++)
                {
                    bll.add(pid, Oid[i]);
                }
                return 1;
            }
            else
            {
                return 0;
            }

        }

    }
}
